//index.js
//获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    motto: "装的一手好B 斗的一手骚图",
    showEyes: "",
    searchValue: null,
    stickerid: "",
    hotimgList: [{
      "img": "",
      "title": ""
    }],
    animateClass: "",
    detailtImg: '/images/img_defalt.jpg',
    //测试数据
    ddd: [{
      'src': '/images/homebanner/banner06.jpg'
    }, {
      'src': '/images/homebaner/banner01.jpg'
    }, {
      'src': '/images/homebaner/banner02.jpg'
    }, {
      'src': '/images/homebaner/banner03.jpg'
    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function() {
    this.refreshHotList();
    this.refreshHotWords();
    this.getMotto();
    //判断有误token
    var userToken = wx.getStorageSync('loginToken');
    if (userToken) {
      console.log('当前缓存token:' + userToken);
    } else {
      app.wxLogin();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    var that = this;
    //随机选一套标语
    this.refreshHotList();
    this.refreshHotWords();

    this.getMotto();
    this.setData({
      animateClass: "fadin"
    });
    setTimeout(function() {
      that.setData({
        animateClass: ""
      });
    }, 500);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

  //显示眼睛
  eyesShow: function() {
    this.setData({
      showEyes: "show-eyes"
    })
  },
  //隐藏眼睛
  eyesHide: function() {
    this.setData({
      showEyes: ""
    })
  },
  //跳转到搜索页面
  toInputSearch: function() {
    if (this.data.searchValue) {
      //链接加关键词参数
      var keywords = this.data.searchValue;
      // var keywords = this.dataset.keywords;
      wx.navigateTo({
        url: '../search/search?keywords=' + keywords
      });
    } else {
      wx.showToast({
        title: '至少打个字吧',
        icon: "none",
        duration: 500
      });
    }
  },
  //跳转到关键词搜索
  toSearch: function(e) {
    var hotci = e.currentTarget.dataset.keywords;
    wx.navigateTo({
      url: '../search/search?keywords=' + hotci
    });
  },
  //校验搜索输入
  searchValueInput: function(e) {
    var value = e.detail.value;
    if (value) {
      //输入不为空内容
      this.setData({
        searchValue: value.replace(/\s+/g, '')
      })
    } else {
      console.log("内容空的")
      this.setData({
        searchValue: null
      })
    }
  },
  toStatement: function() {
    wx.navigateTo({
      url: '../statement/statement'
    });
  },
  //查看表情详细
  toStickerDetail: function(e) {
    let that = this;
    let stickerid = e.currentTarget.dataset.stickerid;
    console.log(stickerid);
    wx.navigateTo({
      url: '../sticker_detail/sticker_detail?stickerid=' + stickerid,
    })
  },
  refreshWords: function() {
    console.log("刷新关键词")
  },
  //图片错误
  imgError: function(e) {
    var errImgIndex = e.currentTarget.dataset.index;
    var currImgSrc = e.currentTarget.dataset.src;
    var ImgList = this.data.ddd;
    var errorSrc = ImgList[ errImgIndex ].src;
    var errorSrc = '111';
    var currImgSrc = errorSrc;
  },
  //获取随机标语
  getMotto: function() {
    var mottoArr = app.globalData.mottoArr;
    var mottos = Math.floor((Math.random() * mottoArr.length));
    this.setData({
      motto: mottoArr[mottos]
    })
  },
  //刷新热门
  refreshHotList: function() {
    var that = this;
    wx.showNavigationBarLoading();
    wx.request({
      url: app.globalData.hotwordUrl, //接口地址
      data: {},
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function(res) {
        wx.setStorage({
          key: 'stickersStorage',
          data: res.data.images,
        })
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
      },
      fail: function() {
        console.log("接口调用失败");
        wx.showToast({
          title: '加载失败',
          icon: 'none'
        });
        wx.stopPullDownRefresh();
      },
      complete:function() {
        console.log("请求完成");
        that.setData({
          hotimgList: wx.getStorageSync('stickersStorage')
        });
      }
    });
  },
  //刷新搜索热词
  refreshHotWords: function() {
    var that = this;
    wx.showNavigationBarLoading();
    wx.request({
      url: app.globalData.hotwordUrl, //接口地址
      data: {},
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function(res) {
        that.setData({
          hotciList: res.data.hotci
        });
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
      },
      fail: function() {
        console.log("接口调用失败");
      }
    });
  }
})